package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.SRException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    DishService dishService;
    /**
     * 新增
     * @param
     */
    @Override
    public void saveAll(SetmealDto setmealDto) {
        //新增套餐及对应套餐的菜品数据
        //首先添加套餐信息 然后再添加对应的菜品信息
        this.save(setmealDto);


        //取出setmeal菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //设置对应的菜品id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //批量添加
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> pageMathSetmeal(Integer page, Integer pageSize, String name) {
        //创建分页对象传入参数
        Page<SetmealDto> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = setmealMapper.pageMathSetmeal(setmealPage,name);
        return setmealDtoPage;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        //首先判断是否在启用状态 如果是就不能删除
        //select count(*) from setmeal where id in (...) and stus = 1;
        Integer count = this.lambdaQuery().in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1).count();
        if(count >0){
            throw new SRException("此商品正在售卖!");
        }

        //如果停用则删除
        //delete from setmeal where id in (...)
        this.removeByIds(Arrays.asList(ids));

        //还要删除套餐对应的菜品id
        //delete from setmeal_dish where setmeal_id in (...)
        //操作套餐菜品中间表
        setmealDishService.update()
                .in("setmeal_id",ids).remove();

    }

    /**
     * 根据id回显
     * @param id
     * @return
     */
    @Override
    public SetmealDto findById(Long id) {
        //调用mapper层方法
        return  setmealMapper.findById(id);
    }

    /**
     * 修改
     * @param setmealDto
     */
    @Override
    public void updateWithSetmeaDto(SetmealDto setmealDto) {
        //根据id修改 套餐表直接修改基本属性
        this.updateById(setmealDto);

        //用户有可能添加几条菜品也可能删除几条,之前留下的菜品有id值,添加后的没有id值,需要先把菜品信息删除完然后再添加
        setmealDishService.update().eq("setmeal_id", setmealDto.getId()).remove();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历数据设置id值
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //之后添加到数据库
        setmealDishService.saveBatch(setmealDishes);
    }

    //查询套餐菜品
    @Override
    public List<DishDto> dishList(Long id) {
       return setmealMapper.dishList(id);
    }
}
